package util

import (
	"fmt"
	"strconv"

	"github.com/cloudwego/hertz/pkg/app"
)

func GetParamUint(c *app.RequestContext, name string) (uint64, error) {
	id := c.Param(name)
	if id == "" {
		return 0, fmt.Errorf("can't find param %s", name)
	}

	ret, err := strconv.ParseUint(id, 10, 64)
	if err != nil {
		return 0, err
	}
	return ret, nil
}

func GetQueryUint(c *app.RequestContext, name string) (uint64, error) {
	out := c.Query(name)
	if out == "" {
		return 0, fmt.Errorf("can't find query %s", name)
	}
	ret, err := strconv.ParseUint(out, 10, 64)
	if err != nil {
		return 0, err
	}
	return ret, nil
}

func GetQueryUintOption(c *app.RequestContext, name string) (*uint64, error) {
	out := c.Query(name)
	if out == "" {
		return nil, nil
	}
	ret, err := strconv.ParseUint(out, 10, 64)
	if err != nil {
		return nil, err
	}
	return &ret, nil
}
